﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace DotNetNuke.Modules.ActiveForums.Controls
    <ToolboxData("<{0}:ForumGroupRepeater runat=server></{0}:ForumGroupRepeater>")> _
    Public Class ForumGroupRepeater
        Inherits ControlsBase
        Public Enum RepeatDirections
            Vertical
            Horizontal
        End Enum
        Private _repeatDirection As RepeatDirections
        Private _repeatColumns As Integer = 1
        Private _headerTemplate As String
        Private _footerTemplate As String
        Private _noResults As String
        Private _toggleBehavior As Integer = 0
        Public Property RepeatDirection() As RepeatDirections
            Get
                Return _repeatDirection
            End Get
            Set(ByVal value As RepeatDirections)
                _repeatDirection = value
            End Set
        End Property
        Public Property RepeatColumns() As Integer
            Get
                Return _repeatColumns
            End Get
            Set(ByVal value As Integer)
                _repeatColumns = value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property HeaderTemplate() As String
            Get
                Return _headerTemplate
            End Get
            Set(ByVal value As String)
                _headerTemplate = value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property FooterTemplate() As String
            Get
                Return _footerTemplate
            End Get
            Set(ByVal value As String)
                _footerTemplate = value
            End Set
        End Property
        <Description("Template for display"), PersistenceMode(PersistenceMode.InnerProperty)> _
        Public Property NoResultsTemplate() As String
            Get
                Return _noResults
            End Get
            Set(ByVal value As String)
                _noResults = value
            End Set
        End Property
        Private Sub ForumGroupRepeater_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Me.EnableViewState = False
        End Sub
        Public Property ToggleBehavior() As Integer
            Get
                Return _toggleBehavior
            End Get
            Set(ByVal value As Integer)
                _toggleBehavior = value
            End Set
        End Property
        Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
            'writer.Write(Text)
            writer.Write(HeaderTemplate)
            Dim i As Integer = 0
            If Not ForumData Is Nothing Then
                Dim tmp As String = DisplayTemplate
                Dim xGroups As System.Xml.XmlNodeList = ForumData.SelectNodes("//groups/group")
                Dim fd As ForumDisplay = Nothing
                For Each xNode As System.Xml.XmlNode In xGroups
                    Dim groupId As Integer = Integer.Parse(xNode.Attributes("groupid").Value.ToString)
                    fd = New ForumDisplay
                    fd.DisplayTemplate = Me.DisplayTemplate
                    fd.ForumGroupId = groupId
                    fd.ControlConfig = Me.ControlConfig
                    fd.ModuleConfiguration = Me.ModuleConfiguration
                    'fd.ForumData = ForumData
                    If i = 0 And ToggleBehavior = 1 Then
                        fd.ToggleBehavior = 0
                    ElseIf i > 0 And ToggleBehavior = 1 Then
                        fd.ToggleBehavior = 1
                    End If
                    Me.Controls.Add(fd)
                    fd.RenderControl(writer)
                    i += 1
                Next
            Else
                writer.Write(NoResultsTemplate)
            End If
            writer.Write(FooterTemplate)
        End Sub


        Private Sub ForumGroupRepeater_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        End Sub
    End Class

End Namespace

